//
//  CommonGoodsSourceVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/26.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "CommonGoodsSourceVC.h"
#import "CommonGoodsSourceCell.h"
#import "SourceListModel.h"
#import "SourceListDetailModel.h"
@interface CommonGoodsSourceVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) CommonGoodsSourceCell *commonGoodsSourceCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@end

@implementation CommonGoodsSourceVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleName:CJLString(@"常发货源")];
    [self setBackLeftButton:@""];
    self.commonGoodsSourceCell = [[CommonGoodsSourceCell alloc]initWithFlex:nil reuseIdentifier:nil];

    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [self.tableView.mj_header beginRefreshing];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {

    
        NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/order",CJBaseUrl);
        NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                                 @"pageSize":@"10",
                                 @"isOftenOrder":@"1",
                                 @"isCancel":@"0"
                                };
//
        WS(ws);
        [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {

            DLog(@"++++++++%@",responseObject);

            NSArray *list = responseObject[@"records"];
            NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//            if (list.count > 0) {

                if ([ws.refreshType isEqualToString:@"down"]) {
                    [ws.modelArray removeAllObjects];
                }

                NSError * error ;
                NSArray *arr = [SourceListModel arrayOfModelsFromDictionaries:list error:&error];
                [ws.modelArray addObjectsFromArray:arr];
      
                [ws.tableView.mj_header endRefreshing];
                [ws.tableView.mj_footer endRefreshing];

                if (ws.modelArray.count == total.intValue) {
                    [ws.tableView.mj_footer endRefreshingWithNoMoreData];
                }

//            }else{
//
//                [ws.tableView.mj_header endRefreshing];
//                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//            }
            [ws.tableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"CommonGoodsSourceCell";
    CommonGoodsSourceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CommonGoodsSourceCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    SourceListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithSourceModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SourceListModel *model = self.modelArray[indexPath.row];
  
//    if (self.blockCommonSoures) {
//        self.blockCommonSoures(model);
//    }
//    [self pop];
    
    WS(ws);
    [CJNetWork getSourcesListDetailDataWithCode:model.code Success:^(id  _Nullable responseObject) {

        if (ws.blockCommonSoures) {
            ws.blockCommonSoures((SourceListDetailModel *)responseObject);
        }
        [ws pop];
    }];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"======");
    
    [self didDeleteSelelctCellAtIndexPath:indexPath];
    
}

//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

// 修改Delete按钮文字为“删除”
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (self.selectType == CarListTypeEidite) {
        return YES;
//    }else{
//       return NO;
////    }
}

//设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
      return NO;
}

- (void)didDeleteSelelctCellAtIndexPath:(NSIndexPath *)indexPath{
    
    WS(ws);
    CJAlertView *alert = [[CJAlertView alloc]initWithTitle:@"" strContent:@"确定要删除该单子吗？" ClickOnSure:^(NSString * _Nonnull strSure) {
        DLog(@"--------");
        [ws deleteOrderAtIndexPath:indexPath];
    } ClickOnCancle:^(NSString * _Nonnull strCancle) {
        DLog(@"-=-=-=-=");
    }];
    
    alert.center = KEYWINDOW.center;
    WLAlertView * alertView = [[WLAlertView alloc]init];
    alertView.containerView = alert;
    alertView.isTouchOtherUndissmiss=YES;
    [alertView show];
}

- (void)deleteOrderAtIndexPath:(NSIndexPath *)indexPath{

        
    SourceListModel *model = self.modelArray[indexPath.row];

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/order/cancelOftenOrder",CJBaseUrl);
    NSDictionary *dicPra = @{
        @"code":[CJControl getThreeUnaryValueStr:model.code]
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] PUT:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
        
        DLog(@"++++++++%@",responseObject);
        [ws.tableView.mj_header beginRefreshing];
            
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}


- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight + kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight  -CJTableToTopHeight ),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }

        };

    }
    
    return _tableView;
}



@end
